
package com.cai310.lottery.task.ticket;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.entity.lottery.PrintInterface;
import com.cai310.lottery.service.lottery.PrintEntityManager;
import com.cai310.lottery.service.lottery.ticket.impl.SynchronizedTicketStateManager;
import com.cai310.lottery.service.lottery.ticket.impl.TicketEntityManager;

/**
 * 票状态同步工作任务
 * @author jack
 *
 */
public class TicketStateSynchronizationTask
{
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    protected TicketEntityManager ticketEntityManager;

    @Autowired
    protected PrintEntityManager printEntityManager;

    @Autowired
    protected SynchronizedTicketStateManager synchronizedTicketStateManager;

    /**
     * 同步任务
     */
    public void runTask()
    {
        List<Long> printInterfaceIds = ticketEntityManager.findPrintinterfaceIdByStateModifyTime();
        Set<Long> idSet = new HashSet<Long>();
        idSet.addAll(printInterfaceIds);
        PrintInterface printInterface = null;
        for (Long id : idSet)
        {
            try
            {
                printInterface = printEntityManager.getPrintInterfaceById(id);
                if (printInterface == null)
                {
                    logger.error("接口表中找不到序列号:" + id + "的记录！");
                }
                else
                {
                    if (printInterface.getLotteryType().equals(Lottery.JCZQ)
                            || printInterface.getLotteryType().equals(Lottery.JCLQ))
                    {
                        synchronizedTicketStateManager.synchronizationState_jc(printInterface);
                    }
                    else
                    {
                        synchronizedTicketStateManager.synchronizationState(printInterface);
                    }
                }
            }
            catch (Exception e)
            {
                logger.error(e.getMessage());
                continue;
            }
        }
        try
        {
            Thread.sleep(1);
        }
        catch (InterruptedException e)
        {
            logger.error("" + Thread.currentThread() + "sleep出错！");
        }
    }
}
